//
//  STDSGLContext.hpp
//  STDRenderSDK
//
//  Created by 陈学明 on 2023/3/13.
//  Copyright © 2023 陈学明. All rights reserved.
//

#ifndef STDSGLContext_hpp
#define STDSGLContext_hpp

extern "C" {
#include "STDSProvider.h"
}

#include <stdio.h>
#include <Basic/Shader/STDSEffectManager.hpp>
#include <Basic/graphic/STDSTexture.hpp>
#include <Basic/graphic/STDSObject.h>
#include <glm/glm.hpp>

class STDSGLContext {
    
public:
    STDSGLContext() {
        programManager = new STDSEffectManager();
    }
    
    ~STDSGLContext() {
        delete programManager;
    }
    
    void initGL() {
        
    }
    
    shared_ptr<STDSTexture> getImageTexture(string &path) {
        if (mProvider == nullptr) {
            return nullptr;
        }
        int width = 0;
        int height = 0;
        unsigned char *data = nullptr;
        mProvider->getImageFunc(path.c_str(), &data, &width, &height);
        if (data == nullptr || width == 0 || height == 0) {
            return nullptr;
        }
        shared_ptr<STDSTexture> texture = make_shared<STDSTexture>(data, width, height);
        free(data);
        return texture;
    }

    string getFontPath(string &name) {
        if (mProvider == nullptr) {
            return nullptr;
        }
        char *cPath = nullptr;
        mProvider->getFontPath(name.c_str(), &cPath);
        if (cPath == nullptr) {
            return "";
        }
        string path(cPath);
        free(cPath);
        return path;
    }

    void unInitGL() {
        
    }
    
    void drawBefore() {
        
    }
    
    void drawAfter() {
        clearAutoReleasePool();
    }
    
    void autoRelease(shared_ptr<STDSObject> obj) {
        if (obj != nullptr) {
            mAutoReleasePool.push_back(obj);
        }
    }
    
public:
    shared_ptr<STDSShaderProgram> getProgram(const string &name) {
        if (programManager == nullptr) {
            return nullptr;
        }
        return programManager->getProgram(name);
    }
    
    void setRegisterProvider(shared_ptr<STDSProvider> provider) {
        mProvider = provider;
    }
    
    void setDuration(float d) {
        mDuration = d;
    }
    
    float getDuration() {
        return mDuration;
    }
    
    void setPrograss(float prograss) {
        mCurPrograss = prograss;
    }
    
    float getPrograss() {
        return mCurPrograss;
    }
    
    void setSizeSRC(glm::vec2 size) {
        mSizeSRC = size;
    }
    
    void setSizeCVS(glm::vec2 size) {
        mSizeCVS = size;
    }
    
    glm::vec2 getSizeSRC() {
        return mSizeSRC;
    }
    
    glm::vec2 getSizeCVS() {
        return mSizeCVS;
    }
    
private:
    void clearAutoReleasePool() {
        mAutoReleasePool.clear();
    }
    
private:
    float mDuration{0.0};
    glm::vec2 mSizeSRC{0, 0};
    glm::vec2 mSizeCVS{0, 0};
    STDSEffectManager *programManager{nullptr};
    shared_ptr<STDSProvider> mProvider{nullptr};
    float mCurPrograss{0.0};
    vector<shared_ptr<STDSObject>> mAutoReleasePool;
};
#endif /* STDSGLContext_hpp */
